var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var circleUrl = require("url/circle");
var http = require("helper/http");
var imHelper = require("helper/im");
// UI
var headerAout = ui("headerAout");
var linearLayout_main = ui("linearLayout_main")

// 加载头部
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
// andriod返回键
page.on("back", function() {
	app.closePage();
});

//页面装载完成后，开始初始化工作
var circleId = null;
var imGroupId = null;
page.on("loaded", function(){
	circleId = page.getData().circleId;
	loadPage();
});

// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
// 右上角-
var headerRight = ui(addheader + ".headerRight");
headerRight.visible = false;
headerLeft.on("touch", function() {
	app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "聊天设置",
})
header.refreshData();

var addSwitch1 = linearLayout_main.add("switch1","source://view/reuse/form/mutiSwitch.ui");
var switch1 = ui("switch1");
switch1.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "消息免打扰"
})
switch1.refreshData();
switch1.on("switchtag", function(data) {
	if (data == 0) {// 关闭
		setMsgTipSilence(false);
	}
	if (data == 1) {// 打开
		setMsgTipSilence(true);
	}
});


function loadPage() {
	http.post(circleUrl.url.base.detail4manage, {id: circleId}, function(data) {
		var imGroup = data.imGroup;
		if(imGroup){
			imGroupId = imGroup.imGroupId;//群聊id
			var cacheSilence = imHelper.cacheMsgSilence(imGroupId);
			if (cacheSilence=="1") {
				switch1.fire("switchzt", [ 1, " ", " " ]);
			}else{
				switch1.fire("switchzt", [ 0, " ", " " ]);
			}
		}
	})
}

/**
 * 设置-消息免打扰
 * @param isSilence 是否静音
 * @returns
 */
function setMsgTipSilence(isSilence) {
	if(imGroupId!=null){
		if(isSilence){
			imHelper.cacheMsgSilence(imGroupId,1);//静音
		}else{
			imHelper.cacheMsgSilence(imGroupId,2);//非静音
		}
	}
}
